package malus.shaofeng.com.beiwomusic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.model.HotPost;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;

/**
 * 获取被窝俱乐部的列表
 */
public class ClubTopicAdapter extends BaseAdapter{
    Context context;
    List<HotPost> data;
    int top;
    public ClubTopicAdapter(Context context,List<HotPost> data){
        if(context!=null){
            this.context=context;
            this.data=data;
        }else{
            throw new IllegalArgumentException("context must not null.");
        }
    }
    @Override
    public int getCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(data!=null){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if(convertView!=null){
            view=convertView;
        }else{
            view= LayoutInflater.from(context).inflate(R.layout.item_bbs_topic_list,parent,false);
        }
        ViewHolder holder=(ViewHolder)view.getTag();
        if(holder==null){
            holder=new ViewHolder();
            holder.title=(TextView)view.findViewById(R.id.bbs_club_list_title);
            holder.nickname=(TextView)view.findViewById(R.id.bbs_club_list_nickname);
            holder.reply=(TextView)view.findViewById(R.id.bbs_club_list_reply);
            holder.toTop=(ImageView)view.findViewById(R.id.bbs_club_list_index_top);
            holder.icon=(ImageView)view.findViewById(R.id.bbs_club_list_icon);
            view.setTag(holder);
        }
        HotPost topic = data.get(position);
        holder.title.setText(topic.getTitle());
        holder.nickname.setText(topic.getUser().getNickname());
        holder.reply.setText(topic.getReplies()+"");
        if(topic.isTop()){
            holder.toTop.setVisibility(View.VISIBLE);
        }else{
            holder.toTop.setVisibility(View.GONE);
        }
        if(topic.getImage_uri()!=null&&topic.getImage_uri().length()>0){
            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
            String imageUrl = BitmapHelper.getImageUrl(topic.getImage_uri());
            bitmapUtils.display(holder.icon, imageUrl);
        }

        return view;
    }
    private class ViewHolder{
        private TextView title;
        private TextView nickname;
        private TextView reply;
        private ImageView toTop;
        private ImageView icon;
    }
}
